﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppToolkit.WPF.UI;
using NUnit.Framework;
using System.Windows;
using System.Windows.Controls;
using UnitTestUtils.UI;
using UnitTestUtils.UI.Input;

namespace AppToolkit.WPF.Tests.UI
{
    [TestFixture, RequiresSTA]
    public class VisualTreeHelperExtTests
    {
        private UIHost _host;

        [TearDown]
        public void Dispose()
        {
            if (_host != null)
            {
                _host.Window.Close();
                _host = null;
            }
            UITestHelper.CleanupDispatcher();
        }

        [Test]
        public void FindRootWindow_returns_Window_if_Window_is_passed()
        {
            Window wnd = new Window();
            Assert.AreSame(wnd, VisualTreeHelperExt.FindRootWindow(wnd));
        }

        [Test]
        public void FindRootWindow_walks_hierarchie_correctly()
        {
            Window wnd = new Window();
            Control ctrl = new Control();
            wnd.Content = ctrl;

            _host = UIHost.Create(wnd);
            
            _host.Show();
            Assert.AreSame(wnd, VisualTreeHelperExt.FindRootWindow(ctrl));
        }

        [Test]
        public void GetParentOfType()
        {
            var wnd = new Window();

            var ctrl0 = new Grid();
            wnd.Content = ctrl0;

            var ctrl1 = new Grid();
            ctrl0.Children.Add(ctrl1);

            _host = UIHost.Create(wnd);

            _host.Show();
            Assert.AreSame(wnd, VisualTreeHelperExt.GetParentOfType(ctrl1, typeof(Window)));
        }

        [Test]
        public void GetParentOfType_generic()
        {
            var wnd = new Window();
            
            var ctrl0 = new Grid();
            wnd.Content = ctrl0;

            var ctrl1 = new Grid();
            ctrl0.Children.Add(ctrl1);

            _host = UIHost.Create(wnd);
            
            _host.Show();
            Assert.AreSame(wnd, VisualTreeHelperExt.GetParentOfType<Window>(ctrl1));
        }

        [Test]
        public void WalkTree()
        {
            var wnd = new Window();

            var ctrl0 = new Grid();
            ctrl0.Name = "TestGrid";
            wnd.Content = ctrl0;

            var ctrl1 = new Grid();
            ctrl0.Children.Add(ctrl1);

            _host = UIHost.Create(wnd);

            _host.Show();

            var pa = PredicateAction.FromType<Window>(window => window.Title="WindowTitle");

            Assert.IsTrue(VisualTreeHelperExt.WalkTree(ctrl1, pa));
            Assert.AreEqual("WindowTitle", wnd.Title);
        }

        //[Test]
        //public void WalkTreeWithCustomFilter()
        //{
        //    var wnd = new Window();

        //    var ctrl0 = new Grid();
        //    ctrl0.Name = "TestGrid";
        //    wnd.Content = ctrl0;

        //    var ctrl1 = new Grid();
        //    ctrl0.Children.Add(ctrl1);

        //    _host = UIHost.Create(wnd);

        //    _host.Show();

        //    var pa = PredicateAction.FromTypeAndFunction<Grid>(p => p.Name == "", g => g. = "WindowTitle");

        //    Assert.IsTrue(VisualTreeHelperExt.WalkTree(ctrl1, pa));
        //    Assert.AreEqual("WindowTitle", wnd.Title);
        //}

        #region FindParent Tests
        //[Test,STAThread]
        //public void GetParentOfType_finds_root_control()
        //{
        //    Window wnd = new Window();
        //    var dataGrid = new DataGrid();
        //    wnd.Content = dataGrid;

        //    DataGridTextColumn textColumn = new DataGridTextColumn();
        //    textColumn.Header = "First Name";
        //    dataGrid.Columns.Add(textColumn);
        //    dataGrid.Items.Add("test");

        //    _host = UIHost.Create(wnd);

        //    _host.Show();

        //    object source = null;
        //    dataGrid.MouseDown += (s, a) =>
        //        {
        //            source = a.OriginalSource;
        //        };

        //    var sim = new MouseSimulator();
        //    var p = dataGrid.PointToScreen(new Point(5, 5));
        //    sim.MoveMouseTo(p.X,p.Y);
        //    sim.LeftButtonDown();
        //    _host.Control.Dispatcher.Flush();

        //    Assert.AreSame(dataGrid, VisualTreeHelperExt.GetParentOfType<DataGrid>((DependencyObject)source));
        //} 
        #endregion
    }
}
